<?php

namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\User;
use App\Models\UserGrowing;
use App\Models\UserAttribute;
use Illuminate\Support\Facades\Input;
use Excel;
class AdminController extends Controller
{
    public function index(Request $request){

		$arr = DB::table("users")->where('is_admin',1)->paginate(10);
        return view('admin.index',['arr'=>$arr]);
    }

	public function uedit(Request $request){
			$id = $request->input("id");
			$re = DB::table("users")->where("id",$id)->get();

			return view("admin.uedit",["detail"=>$re[0]]);
	}
	public function store(Request $request){
		$data = $request->input();
		$user = User::where('id',$data['id'])->first();
		
		if(isset($data['password'])){
			$data['password'] =  bcrypt(md5($data['password']));
			$user->password   = $data['password'];
		}
		if(isset($data['avatar'])){
			$user->avatar = $data['avatar'];
		}
		if(isset($data['push_cover'])){
			$user->push_cover = $data['push_cover'];
		}
		$user->email  = $data['email'];
		$user->status = $data['status'];
		$user->name   = $data['name'];
		$arr = $user->save();
		if($arr){
				return	redirect("admin/list");
		}else{
				return false;
		}
	}
	public function addu(Request $request){
			return view("admin.addu");
	}
	public function addUser(Request $request){
			$data = $request->input();
//			dd($data);

			$data['password'] =  bcrypt(md5($data['password']));
			$data['is_admin'] = 1;
			unset($data['_token']);
			unset($data['_url']);
//			array_pop($data);
			$re = DB::table("users")->insert($data);
			if($re){
					return redirect("admin/list");
			}
	}

	public function det(Request $request){
        $id=$request->input('id');
        $arr=DB::table('users')->where('id',$id)->delete();
        if($arr){
			return	redirect("admin/list");
		}
    }
    public function is_admin(Request $request){
    	$id = $request->input("id");
    	$data['is_admin'] = 1;
    	$arr = DB::table("users")->where('id',$id)->update($data);
    	if($arr){
    		return redirect("admin/list");
    	}
    }
    public function no_admin(Request $request){
    	$id = $request->input("id");
    	$data['is_admin'] = 0;
    	$arr = DB::table("users")->where('id',$id)->update($data);
    	if($arr){
    		return redirect("admin/list");
    	}

    }

    //20181010 平台作者列表
    public function authorList(Request $request){

		$arr = DB::table("users")->where('is_author',1)->paginate(10);
        return view('admin.authorlist',['arr'=>$arr]);
    }

    //20181010 添加平台作者
    public function authorAdd(Request $request){
		
		return view("admin.authoradd");
	}
	//20181010 执行添加平台作者
	public function authorInsert(Request $request){
		$data = $request->input();
		$data['password']  =  bcrypt(md5($data['password']));
		$data['is_author'] = 1;
		unset($data['_token']);
		unset($data['_url']);
		$re = DB::table("users")->insert($data);
		if($re){
			return redirect("admin/authorlist");
		}
	}
	//20181010 编辑作者信息页面
	public function authorEidt(Request $request){
		$id = $request->input("id");
		$re = DB::table("users")->where("id",$id)->get();
		$user_attribute = UserAttribute::where("user_id", $id)->select("remark")->first();
		if($user_attribute){
			$remark = $user_attribute->remark;
		}else{
			$remark = "";
		}
		return view("admin.authoredit",["detail"=>$re[0], "remark"=>$remark]);
	}
	//20181010 更新作者信息
	public function authorUpdate(Request $request){
		$data = $request->input();
		$user = User::where('id',$data['id'])->first();
		
		if(isset($data['password'])){
			$data['password'] =  bcrypt(md5($data['password']));
			$user->password   = $data['password'];
		}
		if(isset($data['avatar'])){
			$user->avatar = $data['avatar'];
		}
		if(isset($data['push_cover'])){
			$user->push_cover = $data['push_cover'];
		}
		//完善用户属性信息
		if(isset($data['remark'])){
			$attribute = UserAttribute::where("user_id", $data['id'])->first();
			if($attribute){
				$attribute->remark = $data['remark'];
				$attribute->save();
			}else{
				$attribute = new UserAttribute();
				$attribute->user_id = $data['id'];
				$attribute->remark  = $data['remark'];
				$attribute->save();
			}
		}
		$user->email  = $data['email'];
		$user->status = $data['status'];
		$user->name   = $data['name'];
		$user->sex    = $data['sex'];
		$arr = $user->save();
		if($arr){
				return	redirect("admin/authorlist");
		}else{
				return false;
		}
	}
	//20181010 执行删除作者信息页面
	public function authorDelete(Request $request){
        $id=$request->input('id');
        $arr=DB::table('users')->where('id',$id)->delete();
        if($arr){
			return	redirect("admin/authorlist");
		}
    }
    //裂变者列表
    public function share(Request $request){
    	$where = array();
    	if (Input::has('user_id')){
    		$where['user_id'] = Input::get('user_id');
    		$where['is_fission'] = Input::get('is_fission');
		}
    	$is_fission = $request->input("is_fission", 1);

		$data = UserAttribute::where("is_fission", $is_fission)->where($where)->select("user_id", "remark")->paginate(15);
		$count = UserAttribute::where("is_fission", $is_fission)->count();

		$this->ret['is_fission'] = $is_fission;
		$this->ret['data'] = $data;
		$this->ret['count']= $count;
    	return view("admin/share",$this->ret);
    }
	public function shareDetail(Request $request,$userid){
		$tel  = $request->input('tel');
		$name = User::where("id",$userid)->select("name")->first();
		$is_fission = $request->input("is_fission");
		
		if($is_fission==1){
			$data = DB::table("users_growing")
					->where("fission_id", $userid)
					->paginate(15);
		}elseif($is_fission==2){
			$code = "20";
        	$data = DB::table("users_growing")
            ->where("top_top_id", $userid)
            ->where("mobile", ">", 0)
            ->where("code", $code)
            ->select("user_id","id","mobile", "created_at", "name")
            ->orderBy("id", "desc")
            ->paginate(15);
		}
		foreach($data as &$item){
			$user = DB::table("users")->where("id", $item->user_id)->select("name", "nickname")->first();
			$tag  = DB::table("users_attribute")->where("user_id", $item->user_id)->select("worktag")->first();
			$item->name = $user->nickname ? $user->nickname : "暂无昵称";
			$item->worktag = $tag->worktag ? $tag->worktag : "---";
		}
		
		$this->ret['is_fission'] = $is_fission;
		$this->ret['data'] = $data;
		$this->ret['tel']  = $tel;
		$this->ret['curname'] = $name->name;
		$this->ret['user_id'] = $userid;
		return view("admin/shareDetail",$this->ret);
	}

	/*
	*20181228
	*
	*/
	public function downloadExcel(Request $request){
		$start_time = $request->input("start");
		$end_time   = $request->input("end");
		$user_id    = $request->input("user_id");
		$username   = $request->input("username");

		$is_fission = DB::table("users_attribute")->where("user_id",$user_id)->select("is_fission")->first();
        if($is_fission->is_fission==2){
            $code  = "20";
            $field = "top_top_id";
        }else{
            $code = "00";
            $field = "fission_id";
        }
		if($start_time && $end_time){
			$list = DB::table("users_growing")
					->where($field, $user_id)
					->where("code", $code)
					->where("mobile", ">", 0)
					->where("created_at", ">=", $start_time)
					->where("created_at", "<=", $end_time)
					->select("user_id","id","mobile", "created_at", "name")
            		->orderBy("id", "desc")
					->get();
		}else{
			$list = DB::table("users_growing")
					->where($field, $user_id)
					->where("code", $code)
					->where("mobile", ">", 0)
					->select("user_id","id","mobile", "created_at", "name")
            		->orderBy("id", "desc")
					->get();
		}
		foreach($list as &$item){
			$user = DB::table("users")->where("id", $item->user_id)->select("name", "nickname")->first();
			$tag  = DB::table("users_attribute")->where("user_id", $item->user_id)->select("worktag")->first();
			$item->name = $user->nickname ? $user->nickname : "暂无昵称";
			$item->worktag = $tag->worktag ? $tag->worktag : "无标签";
		}
		$info = array();
		foreach($list as $k=>$item){
			$info[$k][] = $item->user_id;
			$info[$k][] = $item->name ? $item->name : "暂无姓名";
			$info[$k][] = $item->worktag ? $item->worktag : "无标签";
			$info[$k][] = $item->mobile ? $item->mobile : "暂无手机号";
			$info[$k][] = $item->created_at ? $item->created_at : "暂无时间";
			//echo $item->user_id."__".$item->name."__".$item->mobile."<br/>";
		}
		//return;
		$title      = ['id','姓名','工作标签', '手机号','创建时间'];
		array_unshift($info,$title);
		$data['list'] = $info;
		Excel::create($username."邀请列表",function($excel) use ($info){
            $excel->sheet('score', function($sheet) use ($info){
                $sheet->rows($info);
            });
        })->export('xls');

		dd($list);
	}

	public function fission($userid,$fission){
		$attr = new UserAttribute();
		$re = UserAttribute::where("user_id",$userid)->update(["is_fission"=>$fission]);
		if($re == 0){
			$attr->user_id = $userid;
			$attr->is_fission = $fission;
			$data = $attr->save();
			if($data){
				echo "<script>window.location.href=document.referrer;</script>";
			}else{
				echo "<script>window.location.href=document.referrer;</script>";
			}
		}else{
			echo "<script>window.location.href=document.referrer;</script>";
		}

	}
	//微信名称列表
	public function wechat_name(Request $request){

		$arr = $request->input("name");
		//dd($arr);
		if(Input::has('name')){
			$data = DB::table("wechat_name")->where("url","like","%$arr%")->paginate(15);
			
		}else{
			
			$data = DB::table("wechat_name")->paginate(15);
		}
		return view("admin.wechatName",['data'=>$data,'title'=>$arr]);
	}
	
	
	
	
}